var winW = window.innerWidth;
var movePer = 96/750;//96为删除按钮的宽度
var moveDis = 0;
var moveDisY = 0;
var lastPos = 0;
var lastPosY = 0;
	$(".shoppingList ul li").on("touchstart",function(e){
		lastPos = e.originalEvent.targetTouches[0].clientX;
		lastPosY = e.originalEvent.targetTouches[0].clientY;
		moveDis = 0;
		moveDisY = 0;
	});
	
	$(".shoppingList ul li").on("touchmove",function(e){
	var curPos = e.originalEvent.targetTouches[0].clientX;
	var curPosY = e.originalEvent.targetTouches[0].clientY;
	moveDis +=(curPos - lastPos);
	moveDisY += (curPosY - lastPosY);
	if(Math.abs(moveDisY)>10){
		console.log(111)
		moveDis = 0;
	}
	if(-moveDis>20){
		var dis = -1*movePer*winW;
		liMove(e,dis);
	}
	if(moveDis>5){
		liMove(e,0);
	}
	lastPos = curPos;
	lastPosY = curPosY;
})

//touch.on(".shoppingList ul li","drag",function(e){
//	if(moveDis<=0){
//		var curPos = e.distanceX;
//		if(moveDis==0&&curPos>=lastPos){return};
//		moveDis += curPos-lastPos;
//		moveDis = Math.abs(moveDis)>=movePer*winW?-movePer*winW:moveDis;
//		moveDis = moveDis>0?0:moveDis;
//		liMove(e,moveDis);
//		lastPos = curPos;
//	}
//})
//touch.on(".shoppingList ul li","dragend",function(e){
////	console.log(moveDis,(movePer-.2)*winW,movePer)
//	if(moveDis<0 && (Math.abs(moveDis)<(movePer-.01)*winW)){
//		moveDis = 0;
//		liMove(e,moveDis);
//	}
//})

//li移动函数	
function liMove(e,dis){
	var targetLi;
	if(e.target.nodeName=="LI"){
		targetLi = $(e.target);
		
	}else{
		targetLi = $(e.target).parents("li");
	}
	targetLi.css("transform","translateX("+dis+"px)");
	if(dis<0){
		targetLi.find(".del").show();
	}else{
		targetLi.find(".del").hide();
	}
}

//删除商品
var del = $(".del");
var	shoppingList = $(".shoppingList").find("li");
del.on("mouseup",function(){
	$(this).parents("li").css("transform","translateX(100%)");
	$(this).parents("li").animate(
		{"opacity":0},
		200,
		"swing",
		function(){
			$(this).remove();
			totalMoney();
		}
	)
	
})
//选中条目
$(".sel").on("click",function(){
	var isSel = $(this).parents("li").data("selected");
	$(this).parents("li").data("selected",!isSel);
	isSel = $(this).parents("li").data("selected");
	if(isSel){
		$(this).find("img").show();
	}else{
		$(this).find("img").hide();
	}
	totalMoney();
})
//全选
$(".account .left").on("touchstart",function(){
	var isSel = $(this).data("selected");
	$(this).data("selected",!isSel);
	isSel = $(this).data("selected");
	if(isSel){//全选为true
		$(this).find("img").show();
		//li全为true
		$(".shoppingList .listWrap li").data("selected",true);
//		$(".shoppingList .listWrap li .sel").find("img").attr("src","img/shoppingCart/selected.png");
		$(".shoppingList .listWrap li .sel").find("img").show();
	}else{
		$(this).find("img").hide();
		//li全为false
		$(".shoppingList").find(".listWrap li").data("selected",false);
		$(".shoppingList .listWrap li .sel").find("img").hide();
	}
	totalMoney();
})
//增加数量
$(".shoppingList .count .add").on("touchstart",function(){
	var _s = $(this);
	var num = parseInt(_s.siblings(".num").text());
	num++;
	_s.siblings(".num").text(num);
	totalMoney();
})
//减少数量
$(".shoppingList .count .sub").on("touchstart",function(){
	var _s = $(this);
	var num = parseInt(_s.siblings(".num").text());
	num--;
	if(num<=0){num = 0};
	_s.siblings(".num").text(num);
	totalMoney();
})
//计算总钱数
function totalMoney(){
	var totalM = 0;
	$(".shoppingList .listWrap li").each(function(index,curVal){
		if($(this).data("selected")){
			var price = parseFloat($(this).find(".price").find("span").text());
			var num = parseInt($(this).find(".count .num").text());
			var listMoney = price*num;
			totalM += listMoney;
		}
	})
	totalM = totalM.toFixed(2);
	$(".account .right p span").text(totalM);
}
totalMoney();

